#include <jni.h>
#include "utils.h"
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_xuanyuan_jni_NativeLib_stringFromJNI(
        JNIEnv *env,
        jobject obj) {

    //获取java 对象
    jclass cls = env->GetObjectClass(obj);
    jmethodID mid = env->GetMethodID(cls, "createException", "()V");

    // 判断方法是否存在，不存在方法结束
    if (mid == NULL) {
        return nullptr;
    }

    //执行Java对象中的方法
    env->CallVoidMethod(obj, mid);

    jthrowable exc = env->ExceptionOccurred();
    if (exc) {
        jclass newExcCls;
        env->ExceptionDescribe();
        //清理异常堆栈信息
        env->ExceptionClear();
        newExcCls = env->FindClass("java/lang/Exception");
        if (newExcCls != NULL) {
            // 抛出一个新的 异常
            env->ThrowNew(newExcCls, "thrownNew from C++ code");
        }
    }


    std::string hello = "1";
    bool result = throwByClassName(env, cls);
    if(result){
        hello="0";
    }
    return env->NewStringUTF(hello.c_str());

}